package cn.zqy.demo3.day01.Jdbc;

import cn.zqy.demo3.day01.Utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * * 定义一个方法，查询emp表的数据将其封装为对象，然后装载集合，返回。
 *
 *  问题：w为什么要用util下的Date？
 */
public class Demo08JdbcUtilTest {
    public static void main(String[] args) {
        List<Demo08Emp> li  = new Demo08JdbcUtilTest().findAll();
        System.out.println(li);
    }

    public List<Demo08Emp> findAll() {
        List<Demo08Emp> list = new ArrayList<>();
        Connection con = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            //1.注册驱动
            //2.获取连接
            con = JDBCUtils.getConnection();

            //4.获取执行sql的对象
            statement = con.createStatement();
            //3.定义sql
            String sql = "select * from emp";
            //5.执行sql
            rs = statement.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String ename = rs.getString("ename");
                int job_id = rs.getInt("job_id");
                int mgr = rs.getInt("mgr");
                Date joindate = rs.getDate("joindate");
                double salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                int dept_id = rs.getInt("dept_id");
                list.add(new Demo08Emp(id,
                        ename, job_id, mgr, joindate, salary, bonus, dept_id));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, con, statement);
        }

        return list;
    }
}
